package com.laery0.xixin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.laery0.xixin.mapper.UsersMapper;
import com.laery0.xixin.po.ResponseBean;
import com.laery0.xixin.po.Users;
import com.laery0.xixin.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UsersServiceImpl implements UsersService {
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public ResponseBean<Users> getUsersByUserIdByPass(Users users) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", users.getUserId());
        queryWrapper.eq("password", users.getPassword());
        Users inputUser = usersMapper.selectOne(queryWrapper);
        if(inputUser == null){
            return new ResponseBean<>(601, "用户名或密码错误");
        } else {
            inputUser.setPassword(null);
            return new ResponseBean<>(inputUser);
        }
    }

    @Override
    public ResponseBean<Users> getUsersById(String userId) {
        Users users = usersMapper.selectById(userId);
        if(users == null) {
            return new ResponseBean<>(601, "未找到用户");
        } else {
            users.setPassword(null);
            return new ResponseBean<>(users);
        }
    }

    @Override
    public ResponseBean<Integer> saveUsers(Users users) {
        ResponseBean<Users> usersById = getUsersById(users.getUserId());
        if(usersById.getData() != null) {
            return new ResponseBean<>(601, "用户手机号已存在");
        } else {
            return new ResponseBean<>(usersMapper.insert(users));
        }
    }
}
